package hu.issue.web;

import hu.issue.ejb.MilestoneService;
import hu.issue.entity.Milestone;
import hu.issue.framework.web.Constants;
import hu.issue.framework.web.controller.AbstractFormController;
import hu.issue.framework.web.controller.Inject;
import hu.issue.framework.web.controller.RequiredRole;
import hu.issue.framework.web.validation.ErrorMessages;
import hu.issue.framework.web.validation.RequestValidation;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RequiredRole(Constants.ROLE_PROJECTOWNER)
public class AssignToMilestoneController extends AbstractFormController {
	@Inject
	private MilestoneService milestoneService;

	@Override
	protected void buildModel(final HttpServletRequest request) {
		final List<Milestone> milestones = this.milestoneService.getMilestones();
		request.setAttribute("milestones", milestones);
	}

	@Override
	protected void buildValidation(final RequestValidation validation) {

	}

	@Override
	protected String handleSubmission(final HttpServletRequest request, final HttpServletResponse response, final ErrorMessages messages) throws Exception {
		final int taskId = Integer.parseInt(request.getParameter("taskId"));
		final int milestoneId = Integer.parseInt(request.getParameter("milestoneId"));
		this.milestoneService.assignTaskToMilestone(milestoneId, taskId);
		return "redirect:project.do?projectId=" + request.getParameter("projectId");
	}
}
